<template>
  <div class="goods-container">
    <!-- 左侧图片区域 -->
    <div class="left">
      <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" :id="id" :checked="checked" @change="onCheckBoxChange">
        <!-- 将商品图片包裹于 label 之中，点击图片可以切换“复选框”的选中状态 -->
        <label class="custom-control-label" :for="id">
          <!-- 商品的缩略图 -->
          <img :src="thumb" alt="商品图片" class="thumb" />
        </label>
      </div>

    </div>
    <!-- 右侧信息区域 -->
    <div class="right">
      <!-- 商品名称 -->
      <div class="top">{{title}}</div>
      <div class="bottom">
        <!-- 商品价格 -->
        <div class="price">￥{{price.toFixed(2)}}</div>
        <!-- 商品数量 -->
        <div class="count">{{count}}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'EsGoods',
  emits: ['stateChange'],
  props: {
    // 商品的id
    id: {
      type: [Number, String],
      required: true
    },
    // 商品缩略图
    thumb: {
      type: String,
      required: true
    },
    // 名称
    title: {
      type: String,
      required: true
    },
    // 单价
    price: {
      type: Number,
      required: true
    },
    // 购买数量
    count: {
      type: Number,
      required: true
    },
    // 勾选状态
    checked: {
      type: Boolean,
      required: true
    }
  },
  methods: {
    onCheckBoxChange(e) {
      // console.log(e.target.checked)
      this.$emit('stateChange', {
        id: this.goods_id,
        value: e.target.checked
      })
    }
  }
}
</script>

<style lang="less" scoped>
.goods-container {
  + .goods-container {
    border-top: 1px solid #efefef;
  }
  display: flex;
  padding: 10px;
  // 左侧图片的样式
  .left {
    margin-right: 10px;
    // 商品图片
    .thumb {
      display: block;
      width: 100px;
      height: 100px;
      background-color: #efefef;
    }
  }
  // 右侧商品名称、单价、数量的样式
  .right {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex: 1;
    .top {
      font-weight: bold;
    }
    .bottom {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .price {
        color: red;
        font-weight: bold;
      }
    }
  }
}

.custom-control-label::before,
.custom-control-label::after {
  top: 3.4rem;
}
</style>